package com.cos.controller.admin.dynamic;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.ArrayUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.cos.controller.base.BaseController;
import com.cos.entity.Page;
import com.cos.entity.system.User;
import com.cos.service.admin.dynamic.CompanyCircleService;
import com.cos.service.interfaces.circle.CompanyDynamicService;
import com.cos.util.Const;
import com.cos.util.DateUtil;
import com.cos.util.PageData;
import com.cos.util.ResponseUtil;
import com.cos.util.StringUtil;
import com.cos.util.Tools;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import java.util.ArrayList;
/**
 * @author czw
 * 企业动态的所有信息
 */
@Controller
@RequestMapping(value="/companyCircle")
public class CompanyCircleController extends BaseController{
    
	/**
	 * 菜单地址url(权限用)
	 */
	String menuUrl = "companyCircle/goToInfoList.do";
	
	@Autowired
	private CompanyCircleService companyCircleService ;
	
	/**
	 * 跳转到多级别树页面(内容分类)
	 */
	@RequestMapping(value="/goToInfoList")
	public ModelAndView infoTree() throws Exception{
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		mv.setViewName("admin/informationType/treeList");
		mv.addObject("pd", pd);
		return mv;
	}
	/**
	 * 跳转到多级别树页面(企业分类)
	 */
	@RequestMapping(value="/goToCompanyList")
	public ModelAndView companyTree() throws Exception{
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		mv.setViewName("admin/company/treeList");
		mv.addObject("pd", pd);
		return mv;
	}
	/**
	 * 针对平台业务管理员，查看所有公司发布动态的内容分类
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/listAllInInfoType")
	@ResponseBody
	public String listAllInInfoType(HttpServletResponse response) throws Exception{
		logBefore(logger, "内容分类列表");
		JSONObject group=new JSONObject();
		PageData pd = new PageData();
		List<PageData> list = new ArrayList<PageData>() ;
		List<PageData> newList = new ArrayList<PageData>() ;
		try{
			pd = this.getPageData();
			List<PageData>	varList = companyCircleService.getListAll(pd) ;	//列出列表
			//将获取的内容分类的ID组装为字符串数组
			String[] ArrayDATA_IDS = companyCircleService.groupIDs(varList) ;
			//获取每个分类下面的企业
			list = companyCircleService.getListInInfoType(ArrayDATA_IDS) ;
			newList = companyCircleService.groupTreeInfo(varList, list) ;
		} catch(Exception e){
			logger.error(e.toString(), e);
		} 
		JSONArray rows = JSONArray.fromObject(newList);
		group.put("rows", rows) ;
		ResponseUtil.write(response, rows);
		return null;
	}
	/**
	 * 获取该公司所发布的所有动态信息
	 * @param page
	 * @return
	 */
    @RequestMapping(value="/listDynamic")
	public ModelAndView listDynamic(Page page){
		logBefore(logger, "某一个企业下面的动态列表");
		List<PageData>	varList = new ArrayList<PageData>() ;
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		try{
			pd = this.getPageData();
			String C_ID = pd.getString("ID") ;
			page.setPd(pd);
			//flag :0 代表内容分类下的企业发布的信息  1代表企业发布的信息
			if(pd.getString("flag").equals("0")) {
				varList = companyCircleService.getDynamic(pd) ;	//列出列表
			} else if(pd.getString("flag").equals("1")){
				varList = companyCircleService.getDynamicById(C_ID) ;	//列出列表
			}
			mv.setViewName("admin/dynamic/dynamicList");
			mv.addObject("varList", varList);
			mv.addObject("pd", pd);
			mv.addObject(Const.SESSION_QX,this.getHC());	//按钮权限
		} catch(Exception e){
			logger.error(e.toString(), e);
		}
		return mv;
	}
	/**
	 * 列出所有企业分类下面的企业
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/listAllInCompanyType")
	@ResponseBody
	public String listAllInCompanyType(HttpServletResponse response) throws Exception{
		logBefore(logger, "企业分类列表");
		JSONObject group=new JSONObject();
		PageData pd = new PageData();
		List<PageData> list = new ArrayList<PageData>() ;
		List<PageData> newList = new ArrayList<PageData>() ;
		try{
			pd = this.getPageData();
			List<PageData>	varList = companyCircleService.getCompanyListAll(pd) ;	//列出列表
			//将获取的内容分类的ID组装为字符串数组
			String[] ArrayDATA_IDS = companyCircleService.groupIDs(varList) ;
			//获取每个分类下面的企业
			list = companyCircleService.getListInCompanyType(ArrayDATA_IDS) ;
			newList = companyCircleService.groupTreeCompany(varList, list) ;
		} catch(Exception e){
			logger.error(e.toString(), e);
		} 
		JSONArray rows = JSONArray.fromObject(newList);
		group.put("rows", rows) ;
		ResponseUtil.write(response, rows);
		return null;
	}
	

		/**
		 * 获取动态信息的图片
		 * @author <a href="15083378620@163.com">LXJ</a>
		 * @param response 
		 * @throws Exception
		 */
		@RequestMapping(value="/listDynamicImg")
		public ModelAndView listDynamicImg(Page page) throws Exception{
			logBefore(logger, "获取动态信息的图片");
			ModelAndView mv = this.getModelAndView();
			PageData pd = new PageData();
			pd = this.getPageData();
			try{
				List<PageData> varList = companyCircleService.getDynamicImg(pd);
				mv.setViewName("admin/company/dynamicImg_list");
				mv.addObject("varList", varList);
				mv.addObject("pd", pd);
				mv.addObject(Const.SESSION_QX,this.getHC());
			}catch(Exception e){
				logger.error(e.toString(), e);
			}finally{
				logAfter(logger);
			}
			return mv;
		}
		
		
		
		
		/**
		 * 获取收藏的动态列表
		 * @author <a href="15083378620@163.com">LXJ</a>
		 * @param response 
		 * @throws Exception
		 */
		@RequestMapping(value="/getCompanyList")
		public ModelAndView getCompanyList(Page page) throws Exception{
			logBefore(logger, "获取收藏的动态列表");
			ModelAndView mv = this.getModelAndView();
			PageData pd = new PageData();
			try{	
				pd = this.getPageData();
				page.setPd(pd);
				List<PageData> varList = companyCircleService.getCompanyCollection(page);//获取推荐企业的列表
				mv.setViewName("admin/circle/dynamiccollection_list");
				mv.addObject("varList", varList);
				mv.addObject("pd", pd);
				mv.addObject(Const.SESSION_QX,this.getHC());
			}catch(Exception e){e.printStackTrace();
					logger.error(e.toString(),e);
				}finally{
					logAfter(logger);
				}
				return mv;
		}
		
		
		/**
		 * 关注的企业列表
		 * @param response  用户ID
		 * @throws Exception
		 */
		@RequestMapping(value="/getFocus_Company")
		public ModelAndView getFocusCompanyList(Page page) throws Exception{
			logBefore(logger, "关注的企业列表");
			ModelAndView mv = this.getModelAndView();
			PageData pd = new PageData();
			try{
				pd = this.getPageData();
				page.setPd(pd);
				List<PageData> varList = companyCircleService.getFocus_Company(page);
				mv.setViewName("admin/circle/focuscompany_list");
				mv.addObject("varList", varList);
				mv.addObject("pd", pd);
				mv.addObject(Const.SESSION_QX,this.getHC());
			}catch(Exception e){
				e.printStackTrace();
				logger.error(e.toString()) ;
			}finally{
				logAfter(logger);
			}
			return mv;
		}
		
		/**
		 * 获取推荐的动态列表
		 * @param response 
		 * @throws Exception
		 */
		@RequestMapping(value="/getCompany_Recommend")
		public ModelAndView getCompanyRecommend(Page page) throws Exception{
			logBefore(logger, "获取推荐的动态列表");
			ModelAndView mv = this.getModelAndView();
			PageData pd = new PageData();
			try{	
				pd = this.getPageData();
				page.setPd(pd);
				List<PageData> companyList = new ArrayList<PageData>();
				companyList = companyCircleService.getCompany_Recommend(page); //获取推荐企业的列表
				for(int i=0;i<companyList.size();i++){
				List<PageData> dynamicList =  new ArrayList<PageData>(); 
				dynamicList = companyCircleService.getCompany_Dynamic((String) companyList.get(i).get("ID"));
				companyList.get(i).put("dynamicList", dynamicList); //封装进去最新的两条动态
			}
				mv.setViewName("admin/circle/companyRecommend_list");
				mv.addObject("companyList", companyList);
				mv.addObject("pd", pd);
				mv.addObject(Const.SESSION_QX,this.getHC());
			}catch(Exception e){
				e.printStackTrace();
				logger.error(e.toString()) ;
			}finally{
				logAfter(logger);
			}
			return mv;
		}
	
	
	/* ===============================权限================================== */
	public Map<String, String> getHC(){
		Subject currentUser = SecurityUtils.getSubject();  //shiro管理的session
		Session session = currentUser.getSession();
		return (Map<String, String>)session.getAttribute(Const.SESSION_QX);
	}
	/* ===============================权限================================== */
	
	@InitBinder
	public void initBinder(WebDataBinder binder){
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		binder.registerCustomEditor(Date.class, new CustomDateEditor(format,true));
	}
	
}